source('codes/00_import_libraries.R')
options(scipen=99999999)


#------------------------------------------------------------------------
# Specifications
#------------------------------------------------------------------------

year_grid = data.frame(
  beg = c(1871, 1871, 1950, 2000),
  end = c(2019, 1949, 2019, 2019)
)
period_list = paste0(
  year_grid$beg, '-', year_grid$end
)



#------------------------------------------------------------------------
# Data
#------------------------------------------------------------------------

dict_name = 'base'
dt = readRDS('input/combined_data.rds')[[dict_name]] %>%
  select(ym, mkt0, War, PLS_All_recursive) %>% 
  filter(ym <= 'Oct 2019') %>% 
  mutate(
    across(all_of(starts_with('mkt')), ~ .*12),
    War_l1 = lag(War),
    e_War  = fn_resid(x = War, h = 1), # unexpected component
    E_War  = War - e_War, # expected component
    d_War  = War - lag(War)
  ) %>% 
  drop_na(mkt0)



#------------------------------------------------------------------------
# Corr
#------------------------------------------------------------------------

fn_cor = function(x, panel_name) {
  lapply(1:nrow(year_grid), function(i) {
    df = dt %>% filter(year(ym) %in% year_grid$beg[i]:year_grid$end[i])
    mod = cor.test(df$mkt0, df[[x]])
    c(mod$estimate*100, mod$statistic)
  }) %>% 
    do.call(what = cbind) %>% 
    as_hux() %>% 
    insert_column(c('Correlation', '$t$-stat')) %>% 
    insert_row(panel_name, fill = '') %>% 
    set_top_border(1, everywhere, 0.8) %>% 
    set_bottom_border(final(1), everywhere, 0.8)
}


cor_d = fn_cor('d_War', 'First Difference')
cor_e = fn_cor('e_War', 'AR(1) Residual')
cor_out = rbind(cor_d, cor_e) %>% 
  insert_row('Panel A: Contemporaneous Correlation', fill = '') %>% 
  merge_cells(1, everywhere) %>% 
  set_align(1, everywhere, 'center') %>% 
  set_number_format(1, everywhere, 0) %>% 
  set_number_format(-1, -1, 2) %>% 
  set_tb_borders(1, everywhere, 0.8) %>% 
  set_all_padding(0)



#------------------------------------------------------------------------
# Regression
#------------------------------------------------------------------------

fn_reg = function(x, panel_name) {
  lapply(1:nrow(year_grid), function(i) {
    df = dt %>% filter(year(ym) %in% year_grid$beg[i]:year_grid$end[i])
    mod = fn_fit_ols(df = df, xvar = x, yvar = 'mkt0')
    mod_r = coeftest(mod, vcov. = NeweyWest(mod, lag = 6, prewhite = FALSE))
    mod_r[2, c(1, 3)]
  }) %>% 
    do.call(what = cbind) %>% 
    as_hux() %>% 
    insert_column(c('$\\beta$', '$t$-stat')) %>% 
    insert_row(panel_name, fill = '') %>% 
    set_top_border(1, everywhere, 0.8) %>% 
    set_bottom_border(final(1), everywhere, 0.8)
}

reg_d = fn_reg('d_War', 'First Difference')
reg_e = fn_reg('e_War', 'AR(1) Residual')
reg_out = rbind(reg_d, reg_e) %>% 
  insert_row('Panel B: Contemporaneous Regression', fill = '') %>% 
  merge_cells(1, everywhere) %>% 
  set_align(1, everywhere, 'center') %>% 
  set_number_format(1, everywhere, 0) %>% 
  set_number_format(-1, -1, 2) %>% 
  set_tb_borders(1, everywhere, 0.8) %>% 
  set_all_padding(0)



#------------------------------------------------------------------------
# Export
#------------------------------------------------------------------------

output = rbind(cor_out, reg_out) %>% 
  insert_row(c('', period_list)) %>% 
  set_number_format(1, everywhere, 0) %>% 
  set_tb_borders(1, everywhere, 0.8) %>% 
  set_align(everywhere, -1, 'center') %>%
  set_escape_contents(F)

quick_xlsx(output, file = 'output/tables/Table_10.xlsx')

